<template>
  <div class="avatar_box">
<!--    <el-row :index="it.path"  v-for="it in carlist" :key="it.name" :gutter="20">-->



<!--    <div v-if="it.state=='1'">-->
<!--      <div class="photo">-->
<!--        <span>{{it.name}}</span>-->
<!--        已预约-->
<!--      </div>-->
<!--    </div>-->




<!--        <div class="grid-content bg-purple"></div>-->

<!--    </el-row>-->
    <!--    主体部分-->
    <el-card  class="mainn">

      <el-row :gutter="25">
        <el-col :span="20">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" >
            <el-button slot="append" icon="el-icon-search" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" >添加停车位</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表 -->
      <el-table :data="carlist"  style="hight: 200px"
                border stripe>
        <el-table-column type="index" ></el-table-column>
        <el-table-column label="停车位" prop="name"></el-table-column>
        <el-table-column label="状态" prop="state"></el-table-column>


        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.name)"></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUser(scope.row.name)"></el-button>


          </template>
        </el-table-column>
      </el-table>
      <div>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

    </el-card>
    <!-- 创建新停车位对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%"
               @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules"
               ref="addFormRef" label-width="70px">
        <!-- 停车位 -->
        <el-form-item label="停车位" prop="name">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="状态" prop="state">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>

      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户对话框 -->
    <el-dialog title="修改停车位信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <!-- 用户名 -->
        <el-form-item label="停车位" prop="name" >
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="状态" prop="state">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    created (){
      this.getProduction();
    },
    data(){
      return{
        //查询信息实体
        queryInfo:{
          query:"",//查询信息
          pageNum: 1,//当前页
          pageSize: 5,//每页最大数
        },
        meanlist:[],
        carlist:[],
        userlist:[],//用户列表
        total:0,//总记录数
        addDialogVisible: false,
        // 添加用户表单项
        addForm: {
          username:'',
          password:'',
          email:'',
        },


        // 控制修改用户对话框显示/隐藏
        editDialogVisible:false,
        // 修改用户信息
        editForm:{},
        // 验证规则
        addFormRules:{
          // 添加用户表单验证规则
          username:[
            { required: true, message: "请输入用户名", trigger: "blur" },
            { min: 1, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
          ],
          password:[
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 1, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
          ],
          email:[
            { required: true, message: "请输入邮箱", trigger: "blur" },
            { min: 1, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
          ],
        },
        // 修改用户表单验证规则
        editFormRules:{
          password:[
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
          ],
          email:[
            { required: true, message: "请输入邮箱", trigger: "blur" },
            { min: 5, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
          ],
        },
      }

    },
    methods:{

      async deleteUser(name){
        // 弹框
        console.log(name);
        const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        // 成功删除为confirm 取消为 cancel
        if(confirmResult!='confirm'){
          return this.$message.info("已取消删除");
        }
        const {data:res} = await this.$http.delete("deleteUser?name="+name);
        if (res != "success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        this.getProduction();
      },

      async addUser(){
        this.$refs.addFormRef.validate(async valid=>{
          if( !valid ) return;
          // 发起请求
          const {data:res} = await this.$http.post("addUser",this.addForm);
          if (res != "success") {
            userinfo.state = !userinfo.state;
            return this.$message.error("操作失败！！！");
          }
          this.$message.success("操作成功！！！");
          //隐藏
          this.addDialogVisible = false;
          this.getProduction();
        })
      },
      async addDialogClosed(){
        this.$refs.addFormRef.resetFileds();


      },
      async getProduction() {
        // 调用post请求
        const { data: res } = await this.$http.get("production", {

        });
        this.carlist = res.data; // 将返回数据赋值
        // this.total = res.numbers; // 总个数y
        // console.log(queryInfo);



      },

    }
  }


</script>

<style scoped>

  .el-row{
    margin-bottom: 20px;

  }
  .lefts{
    float: left;
    width: 290px;
    height: 600px;
    background-color: #42b983;
  }
  .left{
    float: left;
    position: relative;
    width: 290px;
    height: 600px;
    background-color: red;
  }
  .rights{
    float: left;
    position: relative;
    width: 290px;
    height: 600px;
    background-color: bisque;
  }
  .avatar_box{
    float: left;
  }
  .right{
    float: left;
    position: relative;
    width: 290px;
    height: 600px;
    background-color: aqua;
  }
</style>
